require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
require 'win32ole'
require 'win32/process'

class TC_ForumGroup_test < Test::Unit::TestCase
   
    def startup
      $hrefid = ""
      $GroupName= 'ForumGroup1'
      $GroupNameEdit = $GroupName+'_Edit'
      $GroupDescription = 'This is the first group for forums'
      $GroupDescriptionEdit = $GroupDescription+'_Edit'
      $ForumName = 'Forum1'
      $ForumNameEdit = $ForumName+'_Edit'
      $ForumDescription = 'This is the first forum'
      $ForumDescriptionEdit = $ForumDescription+'_Edit'
      $AppKey = 'ForumAppKey'
      $AppKeyEdit = $AppKey+'_Edit'
      $Edit = 'Editing Post to test the functionality'
      $PostSubject = 'ForumPost1'
      $PostDescription = 'This is a new Forum Post'
      $PostSubjectEdit =  $PostSubject+'_Edit'
      $PostDescriptionEdit = $PostDescription +'_Edit'
      $UserName = 'User'
      $UserPassword = 'pass123'
      $UserEmail = 'user@user.com'
      $PollTitle = 'Testing Using Watir'
      $PollDescription ='Testing Community Server using Watir fetches results which are ...  '
      $Answer1 ='Good'
      $Answer2 = 'Fine'
      $Answer3 = 'Ok'
      $ExpireAfterDays = '30'
      $Request = 'I was wondering if we can be friends'
      $EmailSubject = 'Hello'
      $EmailContent =  'This is to test the functionality for email in forums'
      $MessageSubject = 'Hi'
      $MessageDescription = 'This is to test the functionality for messaging in the forums'
      $Reply = 'Forum Reply'
      $ReplyContent ='Testing the reply for a forum post'
      $DeleteReason = 'Testing delete option for forum post'
      @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    def test_0000
        require 'win32/process'
        $pid = Process.create(
        :app_name      => 'ruby ../resources/clickers.rb',
        :creation_flags  => Process::DETACHED_PROCESS
        ).process_id
    end  
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
    end
    
    def test_08_navigateToForumAdministration
        ie.goto($SITE_URL + 'controlpanel/forums/')
        assert(ie.contains_text('Forum Administration'))      
    end  
    def test_09_verifyLinksExist
      # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Forum Administration').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
    end
    def test_10_navigateToCreateForumGroup 
        ie.goto($SITE_URL +'controlpanel/forums/ForumGroups.aspx')
        assert(ie.button(:name,/AddButton$/).exists?)
        ie.button(:name,/AddButton$/).click
        assert(ie.text_field(:id,/ForumGroupName$/).exists?)
        ie.text_field(:id,/ForumGroupName$/).value =$GroupName
        sleep 3
        assert(ie.text_field(:id,/ForumGroupDesc$/).exists?)
        ie.text_field(:id,/ForumGroupDesc$/).value =$GroupDescription
        sleep 3
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
    end
    
    def test_11_navigateToCreateForum
         ie.goto($SITE_URL +'controlpanel/forums/SectionEdit.aspx')
        assert(ie.text_field(:id,/Name$/).exists?)
        ie.text_field(:id,/Name$/).value =$ForumName
        sleep 2
        assert(ie.text_field(:id,/Description$/).exists?)
        ie.text_field(:id,/Description$/).value =$ForumDescription
        sleep 2
        assert(ie.select_list(:id,/AdminGroupList$/).exists?)
        sleep 2
        ie.select_list(:id,/AdminGroupList$/).select($GroupName)
        assert(ie.radio(:id,/IsActive_0$/).exists?)
        assert(ie.radio(:id,/IsActive_1$/).exists?)
        ie.radio(:id,/IsActive_0$/).set
        assert(ie.select_list(:id,/DefaultLanguage$/).exists?)
        ie.select_list(:id,/DefaultLanguage$/).select('English')
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
    end   
    def test_12_navigateToReadForum
        assert(ie.table(:id,'Listing').exists?)
        table1 = ie.table(:id, 'Listing')
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$ForumName).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value, 'Read').click
               break
             end   
         rowcount1=rowcount1+1
        end
        sleep 2
      
    end
    def test_13_writePost
        assert(ie.link(:text,'Write a New Post').exists?)
        ie.link(:text,'Write a New Post').click
        sleep 3
        # Compose
        sleep 2
        assert(ie.text_field(:id,/PostForm_ctl05_PostSubject$/).exists?)
        ie.text_field(:id,/PostForm_ctl05_PostSubject$/).value =$PostSubject
        sleep 2
        ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_ctl00_bcr_bcr_PostForm_ctl05_PostBody_ctl00_Editor').execCommand('mceSetContent', false,'"+$PostDescription+"')")
        # Options
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_bcr_bcr_PostForm_ctl05_EditorTabs.SelectTab(ctl00_ctl00_bcr_bcr_PostForm_ctl05_EditorTabs.GetTabAtIndex(1))")
        assert(ie.checkbox(:id,/IsLocked$/).exists?)
        ie.checkbox(:id,/IsLocked$/).clear
        assert(ie.select_list(:id,/PinnedPost$/).exists?)
        sleep 2
        ie.select_list(:id,/PinnedPost$/).select('6 Months')
        # Poll
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_bcr_bcr_PostForm_ctl05_EditorTabs.SelectTab(ctl00_ctl00_bcr_bcr_PostForm_ctl05_EditorTabs.GetTabAtIndex(2))")
        assert(ie.checkbox(:id,/IncludePoll$/).exists?)
        ie.checkbox(:id,/IncludePoll$/).set
        sleep 2
        assert(ie.text_field(:id,/PollTitle$/).exists?)
        ie.text_field(:id,/PollTitle$/).value =$PollTitle
        sleep 2
        ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_ctl00_bcr_bcr_PostForm_ctl05_PollSubForm_ctl00_PollContainer_ctl00_PollDescription_ctl00_Editor').execCommand('mceSetContent', false,'"+$PollDescription+"')")
        sleep 2
        assert(ie.text_field(:id,/NewAnswer$/).exists?)
        sleep 2
        ie.text_field(:id,/NewAnswer$/).value =$Answer1
        sleep 2
        if(ie.button(:id,/PollContainer_ctl00_AddButton$/).exists?)
        ie.button(:id,/PollContainer_ctl00_AddButton$/).click
        else
        ie.link(:id,/PollContainer_ctl00_AddButton$/).click
        end
        sleep 2
        assert(ie.text_field(:id,/NewAnswer$/).exists?)
        ie.text_field(:id,/NewAnswer$/).value =$Answer2
         sleep 2
        if(ie.button(:id,/PollContainer_ctl00_AddButton$/).exists?)
        ie.button(:id,/PollContainer_ctl00_AddButton$/).click
        else
        ie.link(:id,/PollContainer_ctl00_AddButton$/).click
        end
        sleep 2
        assert(ie.text_field(:id,/NewAnswer$/).exists?)
        sleep 2
        ie.text_field(:id,/NewAnswer$/).value =$Answer3
        sleep 2
        if(ie.button(:id,/PollContainer_ctl00_AddButton$/).exists?)
        ie.button(:id,/PollContainer_ctl00_AddButton$/).click
        else
        ie.link(:id,/PollContainer_ctl00_AddButton$/).click
        end
        sleep 2
        assert(ie.text_field(:id,/ExpireAfterDays$/).exists?)
        ie.text_field(:id,/ExpireAfterDays$/).value =$ExpireAfterDays
        # Related 
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_bcr_bcr_PostForm_ctl05_EditorTabs.SelectTab(ctl00_ctl00_bcr_bcr_PostForm_ctl05_EditorTabs.GetTabAtIndex(3))")
        sleep 3
        # Preview
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_bcr_bcr_PostForm_ctl05_EditorTabs.SelectTab(ctl00_ctl00_bcr_bcr_PostForm_ctl05_EditorTabs.GetTabAtIndex(4))")
        sleep 5
        if(ie.button(:id,/PostForm_ctl05_PostButton$/).exists?)
        ie.button(:id,/PostForm_ctl05_PostButton$/).click
        else
        ie.link(:id,/PostForm_ctl05_PostButton$/).click
        end
      end
      
    def test_14_moderatePost
        assert(ie.link(:text,'Moderate').exists?)
        ie.link(:text,'Moderate').click
        sleep 5
        assert(ie.cell(:text,'Move').exists?)
        sleep 3
        ie.cell(:text,'Move').click
        sleep 5
        assert(ie.button(:id,/CancelMove$/).exists?)
        sleep 5
        ie.button(:id,/CancelMove$/).click
        sleep 3
        assert(ie.link(:text,$PostSubject).exists?)
        ie.link(:text,$PostSubject).click
        assert(ie.link(:text,'Moderate').exists?)
        ie.link(:text,'Moderate').click
        sleep 5
        assert(ie.cell(:text,'Edit Post').exists?)
        sleep 5
        ie.cell(:text,'Edit Post').click
        sleep 5
        assert(ie.text_field(:id,/PostForm_ctl05_PostSubject$/).exists?)
        ie.text_field(:id,/PostForm_ctl05_PostSubject$/).value =$PostSubjectEdit
        ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_ctl00_bcr_bcr_PostForm_ctl05_PostBody_ctl00_Editor').execCommand('mceSetContent', false,'"+$PostDescriptionEdit+"')")
        sleep 5
        assert(ie.text_field(:id,/EditNotesBody$/).exists?)
        sleep 3
        ie.text_field(:id,/EditNotesBody$/).value =$Edit
        sleep 2
        if(ie.button(:id,/PostForm_ctl05_PostButton$/).exists?)
        ie.button(:id,/PostForm_ctl05_PostButton$/).click
        else
        ie.link(:id,/PostForm_ctl05_PostButton$/).click
        end
      end
      
    def test_15_createUser
        ie.goto($SITE_URL +'controlpanel/membership/UserAdd.aspx')
        assert(ie.text_field(:id,/username$/).exists?)
        ie.text_field(:id,/username$/).value = $UserName
        assert(ie.text_field(:id,/password$/).exists?)
        ie.text_field(:id,/password$/).value = $UserPassword
        password=ie.text_field(:id,/password$/).value
        assert(ie.text_field(:id,/password2$/).exists?)
        ie.text_field(:id,/password2$/).value =password
        assert(ie.text_field(:id,/email$/).exists?)
        ie.text_field(:id,/email$/).value = $UserEmail
        assert(ie.select_list(:id,/timezone$/).exists?)
        ie.select_list(:id,/timezone$/).select('(GMT +05:00) Islamabad, Karachi, Tashkent')
        assert(ie.button(:name,/createButton$/).exists?)
        ie.button(:name,/createButton$/).click
      end
      
    def test_16_navigateToPost
        test_02_verifyLoginLinksExist
        test_03_navigateToLogin
        ie.text_field(:id, /username$/).value = $UserName
        ie.text_field(:id, /password$/).value = $UserPassword
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        sleep 2
        ie.goto($SITE_URL+'forums/')
        sleep 2
        assert(ie.link(:text,$ForumName).exists?)
        ie.link(:text,$ForumName).click
        sleep 2
        assert(ie.link(:text,$PostSubjectEdit).exists?)
        ie.link(:text,$PostSubjectEdit).click
        sleep 5
      end
      
    def test_17_contact
        ie.link(:text, 'Contact').click
        sleep 5
        assert(ie.cell(:text, 'Add admin as a friend').exists?)
        sleep 3
        ie.cell(:text, 'Add admin as a friend').click   
        sleep 3
        ie.ie.Document.ParentWindow.execscript("Telligent_Modal._modalIframe.contentWindow.tinyMCE.get('ctl00_bcr_ctl01_ctl02_Message_ctl00_Editor').execCommand('mceSetContent', false,'"+$Request+"')")
        assert(ie.frame(:index,1).button(:id,/SendButton$/).exists?)
        ie.frame(:index,1).button(:id,/SendButton$/).click
        sleep 3
        ie.link(:text, 'Contact').click
        sleep 5
        assert(ie.cell(:text, 'Remove admin from my friend list').exists?)
        sleep 3
        ie.cell(:text, 'Remove admin from my friend list').click   
        sleep 5
        ie.link(:text, 'Contact').click
        sleep 5
        assert(ie.cell(:text, 'Send admin an email').exists?)
        sleep 3
        ie.cell(:text, 'Send admin an email').click   
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,'ctl00_bcr_ctl01_ctl00_Subject').exists?)
        ie.frame(:index,1).text_field(:id,'ctl00_bcr_ctl01_ctl00_Subject').value =$EmailSubject
        ie.ie.Document.ParentWindow.execscript("Telligent_Modal._modalIframe.contentWindow.tinyMCE.get('ctl00_bcr_ctl01_ctl00_Message_ctl00_Editor').execCommand('mceSetContent', false,'"+$EmailContent+"')")
        #ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_bcr_ctl01_ctl00_Message_ctl00_Editor').execCommand('mceSetContent', false,'"+$EmailContent+"')")
        assert(ie.frame(:index,1).button(:id,/SendButton$/).exists?)
        ie.frame(:index,1).button(:id,/SendButton$/).click
        sleep 5
        ie.link(:text, 'Contact').click
        sleep 5
        assert(ie.cell(:text, 'Start a conversation with admin').exists?)
        sleep 3
        ie.cell(:text, 'Start a conversation with admin' ).click   
        sleep 5
        assert(ie.frame(:index,1).text_field(:id,'ctl00_bcr_CreateConversationForm_ctl02_MessageSubject').exists?)
        ie.frame(:index,1).text_field(:id,'ctl00_bcr_CreateConversationForm_ctl02_MessageSubject').set($MessageSubject)
        sleep 2
        ie.ie.Document.ParentWindow.execscript("Telligent_Modal._modalIframe.contentWindow.tinyMCE.get('ctl00_bcr_CreateConversationForm_ctl02_Message_ctl00_Editor').execCommand('mceSetContent', false,'"+$MessageDescription+"')")
        #ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_bcr_CreateConversationForm_ctl02_Message_ctl00_Editor').execCommand('mceSetContent', false,'"+$MessageDescription+"')")
        assert(ie.frame(:index,1).button(:id,/SaveButton$/).exists?)
        ie.frame(:index,1).button(:id,/SaveButton$/).click
        sleep 3
        ie.link(:text,'Contact').click
        sleep 5
        assert(ie.cell(:text,"View admin's profile").exists?)
        sleep 3
        ie.cell(:text,"View admin's profile").click
        sleep 5 
        ie.goto($SITE_URL+'forums/')
        sleep 2
        assert(ie.link(:text,$ForumName).exists?)
        ie.link(:text,$ForumName).click
        sleep 2
        assert(ie.link(:text,$PostSubjectEdit).exists?)
        ie.link(:text,$PostSubjectEdit).click
        sleep 5
        ie.link(:text, 'Contact').click
        sleep 5
        assert(ie.cell(:text, 'Find posts by admin').exists?)
        sleep 3
        ie.cell(:text, 'Find posts by admin').click   
        sleep 5
        ie.goto($SITE_URL+'forums/')
        sleep 2
        assert(ie.link(:text,$ForumName).exists?)
        ie.link(:text,$ForumName).click
        sleep 2
        assert(ie.link(:text,$PostSubjectEdit).exists?)
        ie.link(:text,$PostSubjectEdit).click
        sleep 5
      end
      
      def test_18_replyPost
      assert(ie.link(:text,'Reply').exists?)
      ie.link(:text,'Reply').click
      sleep 5
      ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_ctl00_bcr_bcr_PostForm_ctl05_PostBody_ctl00_Editor').execCommand('mceSetContent', false,'"+$ReplyContent+"')")
      if(ie.button(:id,/PostButton$/).exists?)
      ie.button(:id,/PostButton$/).click 
      else
      ie.link(:id,/PostButton$/).click 
      end
      sleep 5
    end
    
    def test_19_deletePost
      test_02_verifyLoginLinksExist
      test_03_navigateToLogin
      test_04_populateLoginForm
      test_05_submitLoginForm
      sleep 5
      ie.goto($SITE_URL+'forums/')
      sleep 2
      assert(ie.link(:text,$ForumName).exists?)
      ie.link(:text,$ForumName).click
      sleep 2
      assert(ie.link(:text,$PostSubjectEdit).exists?)
      ie.link(:text,$PostSubjectEdit).click
      sleep 5
      assert(ie.ul(:class,'ForumPostList').li(:index,1).link(:text,'Delete').exists?)
      ie.ul(:class,'ForumPostList').li(:index,1).link(:text,'Delete').click
      sleep 5
      assert(ie.text_field(:id,/DeleteReason$/).exists?)
      sleep 5
      ie.text_field(:id,/DeleteReason$/).value = $DeleteReason 
      sleep 5
      assert(ie.button(:id,/DeletePost$/).exists?)
      ie.button(:id,/DeletePost$/).click 
    end
     
    def test_20_DeleteForum
      ie.goto($SITE_URL+'controlpanel/forums/Forums.aspx')
      assert(ie.table(:id,'Listing').exists?)
      table1 = ie.table(:id, 'Listing')
      rowcount = table1.row_count
      rowcount1 =1
      while rowcount1<=rowcount
         if (table1[rowcount1][1].link(:text,$ForumName).exists?)
          table1[rowcount1][5].button(:id, /DeleteButton$/).click
          break
        end   
        rowcount1=rowcount1+1
      end
      end
      
    def test_21_DeleteForumGroup
      ie.goto($SITE_URL+'controlpanel/forums/ForumGroups.aspx')
      assert(ie.table(:id,'Listing').exists?)
      table1 = ie.table(:id, 'Listing')
      rowcount = table1.row_count
      rowcount1 =1
      while rowcount1<=rowcount
          if (table1[rowcount1][1].link(:text,$GroupName).exists?)
               table1[rowcount1][2].button(:value, 'Delete').click
               break
             end   
      rowcount1=rowcount1+1
      end
    end
    
     def test_22_navigateToDeleteUser
        ie.goto($SITE_URL +'controlpanel/membership/membersearch.aspx?SelectedNavItem=BrowseMembers')
        assert(ie.text_field(:id,/searchText$/).exists?)
        ie.text_field(:id,/searchText$/).value ="*"
        assert(ie.select_list(:name,/PageList$/).exists?)
        ie.select_list(:name,/PageList$/).select('10')
        sleep 3
        assert(ie.button(:id,/searchButton$/).exists?)
        sleep 2
        ie.button(:id,/searchButton$/).click
        sleep 5
        table1 = ie.table(:index, 6)
        rowcount = table1.row_count
        rowcount1 =1
        ok =0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$UserName).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value, 'Delete').click
               sleep 3
               break
             end   
         rowcount1=rowcount1+1
        end
       if(ok ==0)
         sleep 5
         ie.link(:text,'Next >').click
         sleep 5
         test_22_navigateToDeleteUser
       end
       sleep 5
       assert(ie.frame(:index,1).radio(:id,/AssignToAnonymous$/).exists?)
       ie.frame(:index,1).radio(:id,/AssignToAnonymous$/).set
       sleep 3
       assert(ie.frame(:index,1).button(:id,/DeleteUserButton$/).exists?)
       sleep 3
       ie.frame(:index,1).button(:id,/DeleteUserButton$/).click
       sleep 5
       ie.goto($SITE_URL +'controlpanel/membership/membersearch.aspx?SelectedNavItem=BrowseMembers')
       assert(ie.text_field(:id,/searchText$/).exists?)
       ie.text_field(:id,/searchText$/).value ="*"
       sleep 3
       assert(ie.button(:id,/searchButton$/).exists?)
       sleep 2
       ie.button(:id,/searchButton$/).click  
       if(ie.link(:text,$UserName).exists?)
        ie.link(:text,$UserName).click
       end
    end
  
    def test_99_Exit
        ie.close
      end
      
      
end